<template>
    <div class="forget-wrap">
        <div class="ms-forget">
            <div class="ms-title">后台管理系统</div>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="check_password">
                    <el-input type="text" v-model="ruleForm.check_password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input type="text" v-model="ruleForm.email" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item v-if="this.is_email == true">
                    <el-button @click="get_code">获取验证码</el-button>
                </el-form-item>
                <el-form-item label="验证码" prop="code">
                    <el-input type="text" v-model="ruleForm.code" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">立即找回</el-button>
                    <el-button type="primary" @click="login">返回登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { email_code, update_pass } from '../../api/api.js';
export default {
    name: 'login',
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入用户名'));
            } else {
                if (this.ruleForm.password !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入新密码'));
            } else {
                callback();
            }
        };
        var validatePass3 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请确认密码'));
            } else {
                if (value != this.ruleForm.password) {
                    callback(new Error('两次密码输入不一致'));
                }
                callback();
            }
        };
        var validatePass4 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入邮箱'));
            } else {
                var reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
                if (!reg.test(value)) {
                    callback(new Error('请输入有效的邮箱'));
                } else {
                    callback();
                    this.is_email = true;
                }
            }
        };
        var validatePass5 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入验证码'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                username: '',
                password: '',
                check_password: '',
                email: '',
                code: ''
            },
            rules: {
                username: [{ validator: validatePass, trigger: 'blur' }],
                password: [{ validator: validatePass2, trigger: 'blur' }],
                check_password: [{ validator: validatePass3, trigger: 'blur' }],
                email: [{ validator: validatePass4, trigger: 'blur' }],
                code: [{ validator: validatePass5, trigger: 'blur' }]
            },
            is_email: false
        };
    },
    methods: {
        //  找回密码
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    update_pass({
                        username: this.ruleForm.username,
                        password: this.ruleForm.password,
                        code: this.ruleForm.code
                    }).then(res => {
                        if (res.data.code == 200) {
                            this.$message('修改密码成功');
                            this.$router.push('/login');
                        } else {
                            this.$message(res.data.msg);
                        }
                    });
                } else {
                    return false;
                }
            });
        },
        // 获取验证码
        get_code() {
            if (this.ruleForm.username) {
                if (this.ruleForm.email) {
                    email_code({
                        params: {
                            username: this.ruleForm.username,
                            email: this.ruleForm.email
                        }
                    }).then(res => {
                        if (res.data.code == 200) {
                            this.$message('验证码已发送，请注意查收');
                        } else {
                            this.$message(res.data.msg);
                        }
                    });
                }
            } else {
                this.$message('请输入用户名');
            }
        },
        login() {
            this.$router.push('/login');
        }
    }
};
</script>

<style scoped>
.forget-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-forget {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.forget-btn {
    text-align: center;
}
.forget-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.forget-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>